//
//  OrderHomeCell.m
//  DiningCar-Business
//
//  Created by 吕晶 on 2020/4/9.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "OrderHomeCell.h"
#import "OrderMainModel.h"

@interface OrderHomeCell ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *nameLab;//姓名
@property (nonatomic, strong) UILabel *genderLab;//性别
@property (nonatomic, strong) UILabel *phoneLab;//联系方式
@property (nonatomic, strong) UIButton *juJueBtn;//拒绝
@property (nonatomic, strong) UIButton *chuDanBtn;//出单
@property (nonatomic, strong) UIButton *jieDanBtn;//接单
@property (nonatomic, strong) UILabel *marginLine1;
@property (nonatomic, strong) UIView *codeView;//取餐码view
@property (nonatomic, strong) UILabel *codeLab;//取餐码
@property (nonatomic, strong) UILabel *codeTitleLab;//取餐码标题
@property (nonatomic, strong) UILabel *goodTitleLab;//商品信息标题
@property (nonatomic, strong) OrderGoodView *goodView;//商品信息view
@property (nonatomic, strong) UIView *remarkView;//备注view
@property (nonatomic, strong) UILabel *remarkLine;//备注黄线
@property (nonatomic, strong) UILabel *remarkTitleLab;//备注标题
@property (nonatomic, strong) UILabel *remarkLab;//备注
@property (nonatomic, strong) UILabel *marginLine2;
@property (nonatomic, strong) UILabel *xiaoJiTitleLab;//小计标题
@property (nonatomic, strong) UILabel *priceLab;//小计金额
@property (nonatomic, strong) UILabel *pingTaiTitleLab;//平台服务费标题
@property (nonatomic, strong) UILabel *pingTaiLab;//服务费金额
@property (nonatomic, strong) UILabel *shouRuTitleLab;//收入标题
@property (nonatomic, strong) UILabel *shouRuLab;//收入金额
@property (nonatomic, strong) UIView *orderStatusView;//订单状态view
@property (nonatomic, strong) UILabel *infoLab;//所属餐车和下单时间
@property (nonatomic, strong) UILabel *numberLab;//订单编号

@end

@implementation OrderHomeCell

+ (instancetype)OrderHomeCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"OrderHomeCell";
    OrderHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[OrderHomeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = Color_backgroundColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.bgView = [UIView new];
        self.bgView.backgroundColor = [UIColor whiteColor];
        self.bgView.layer.cornerRadius = 4;
        [self addSubview:self.bgView];
        
        self.nameLab = [UILabel new];
        self.nameLab.textColor = HexColor(0x2B2C44);
        self.nameLab.font = BoldFontOfSize(18);
        [self.bgView addSubview:self.nameLab];
        
        self.genderLab = [UILabel new];
        self.genderLab.textColor = HexColor(0xBFC0D1);
        self.genderLab.font = [UIFont systemFontOfSize:12];
        [self.bgView addSubview:self.genderLab];
        
        self.phoneLab = [UILabel new];
        self.phoneLab.textColor = HexColor(0x585974);
        self.phoneLab.font = [UIFont systemFontOfSize:14];
        [self.bgView addSubview:self.phoneLab];
        
        self.juJueBtn = [UIButton new];
        [self.juJueBtn setTitle:@"拒绝" forState:0];
        [self.juJueBtn setTitleColor:HexColor(0x2B2C44) forState:0];
        self.juJueBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        self.juJueBtn.layer.cornerRadius = 4;
        self.juJueBtn.layer.borderWidth = 1;
        self.juJueBtn.layer.borderColor = HexColor(0xBFC0D1).CGColor;
        [self.juJueBtn addTarget:self action:@selector(juJueClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView addSubview:self.juJueBtn];
        
        self.jieDanBtn = [UIButton new];
        [self.jieDanBtn setTitle:@"接单" forState:0];
        [self.jieDanBtn setBackgroundColor:HexColor(0x3D3E5A)];
        [self.jieDanBtn setTitleColor:HexColor(0xffffff) forState:0];
        self.jieDanBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        self.jieDanBtn.layer.cornerRadius = 4;
        [self.jieDanBtn addTarget:self action:@selector(jieDanClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView addSubview:self.jieDanBtn];
        
        self.chuDanBtn = [UIButton new];
        [self.chuDanBtn setTitle:@"出单" forState:0];
        [self.chuDanBtn setBackgroundColor:HexColor(0x3D3E5A)];
        [self.chuDanBtn setTitleColor:HexColor(0xffffff) forState:0];
        self.chuDanBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        self.chuDanBtn.layer.cornerRadius = 4;
        [self.chuDanBtn addTarget:self action:@selector(chuDanClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView addSubview:self.chuDanBtn];
        
        self.marginLine1 = [UILabel new];
        self.marginLine1.backgroundColor = HexColor(0xEAEAF1);
        [self.bgView addSubview:self.marginLine1];
        
        self.codeView = [UIView new];
        self.codeView.backgroundColor = HexColor(0xF9F9FC);
        [self.bgView addSubview:self.codeView];
        
        self.codeLab = [UILabel new];
        self.codeLab.textColor = HexColor(0x2B2C44);
        self.codeLab.font = BoldFontOfSize(20);
        [self.bgView addSubview:self.codeLab];
        
        self.codeTitleLab = [UILabel new];
        self.codeTitleLab.textColor = HexColor(0x585974);
        self.codeTitleLab.font = [UIFont systemFontOfSize:12];
        [self.bgView addSubview:self.codeTitleLab];
        
        self.goodTitleLab = [UILabel new];
        self.goodTitleLab.textColor = HexColor(0x2B2C44);
        self.goodTitleLab.font = BoldFontOfSize(14);
        [self.bgView addSubview:self.goodTitleLab];
        
        self.goodView = [OrderGoodView new];
        [self.bgView addSubview:self.goodView];
        
        self.remarkView = [UIView new];
        self.remarkView.backgroundColor = HexColor(0xFFFBF5);
        [self.bgView addSubview:self.remarkView];
        
        self.remarkLine = [UILabel new];
        self.remarkLine.backgroundColor = HexColor(0xF89800);
        [self.bgView addSubview:self.remarkLine];
        
        self.remarkTitleLab = [UILabel new];
        self.remarkTitleLab.textColor = HexColor(0xF89800);
        self.remarkTitleLab.font = [UIFont systemFontOfSize:12];
        [self.bgView addSubview:self.remarkTitleLab];
        
        self.remarkLab = [UILabel new];
        self.remarkLab.textColor = HexColor(0x9E9FB5);
        self.remarkLab.font = [UIFont systemFontOfSize:12];
        self.remarkLab.numberOfLines = 0;
        [self.bgView addSubview:self.remarkLab];
        
        self.marginLine2 = [UILabel new];
        self.marginLine2.backgroundColor = HexColor(0xEAEAF1);
        [self.bgView addSubview:self.marginLine2];
        
        self.xiaoJiTitleLab = [UILabel new];
        self.xiaoJiTitleLab.textColor = HexColor(0x2B2C44);
        self.xiaoJiTitleLab.font = BoldFontOfSize(14);
        [self.bgView addSubview:self.xiaoJiTitleLab];
        
        self.priceLab = [UILabel new];
        self.priceLab.textColor = HexColor(0x2B2C44);
        self.priceLab.font = BoldFontOfSize(14);
        self.priceLab.textAlignment = NSTextAlignmentRight;
        [self.bgView addSubview:self.priceLab];
        
        self.pingTaiTitleLab = [UILabel new];
        self.pingTaiTitleLab.textColor = HexColor(0x585974);
        self.pingTaiTitleLab.font = [UIFont systemFontOfSize:14];
        [self.bgView addSubview:self.pingTaiTitleLab];
        
        self.pingTaiLab = [UILabel new];
        self.pingTaiLab.textColor = HexColor(0x585974);
        self.pingTaiLab.font = [UIFont systemFontOfSize:14];
        self.pingTaiLab.textAlignment = NSTextAlignmentRight;
        [self.bgView addSubview:self.pingTaiLab];
        
        self.shouRuTitleLab = [UILabel new];
        self.shouRuTitleLab.textColor = HexColor(0x585974);
        self.shouRuTitleLab.font = [UIFont systemFontOfSize:14];
        [self.bgView addSubview:self.shouRuTitleLab];
        
        self.shouRuLab = [UILabel new];
        self.shouRuLab.textColor = HexColor(0x585974);
        self.shouRuLab.font = [UIFont systemFontOfSize:14];
        self.shouRuLab.textAlignment = NSTextAlignmentRight;
        [self.bgView addSubview:self.shouRuLab];
        
        self.orderStatusView = [UIView new];
        self.orderStatusView.backgroundColor = HexColor(0xF9F9FC);
        [self.bgView addSubview:self.orderStatusView];
        
        self.infoLab = [UILabel new];
        self.infoLab.textColor = HexColor(0x9E9FB5);
        self.infoLab.font = [UIFont systemFontOfSize:12];
        [self.bgView addSubview:self.infoLab];
        
        self.numberLab = [UILabel new];
        self.numberLab.textColor = HexColor(0x9E9FB5);
        self.numberLab.font = [UIFont systemFontOfSize:12];
        [self.bgView addSubview:self.numberLab];
    }
    return self;
}

//拒绝
- (void)juJueClick {
    if (self.juJueBlock) {
        self.juJueBlock();
    }
}

//接单
- (void)jieDanClick {
    if (self.jieDanBlock) {
        self.jieDanBlock();
    }
}

//出单
- (void)chuDanClick {
    if (self.chuDanBlock) {
        self.chuDanBlock();
    }
}

-(void)setOrderModel:(OrderMainModel *)orderModel {
    _orderModel = orderModel;
    [self configUI];
}

- (void)configUI {
    self.bgView.nim_left = 15;
    self.bgView.nim_top = 10;
    self.bgView.nim_width = ScreenW - 2 * self.bgView.nim_left;
    
    self.nameLab.text = self.orderModel.buyer_name;
    self.nameLab.nim_left = 10;
    self.nameLab.nim_top = 20;
    [self.nameLab sizeToFit];
    
    if ([self.orderModel.gender isEqualToString:@"1"]) {
        self.genderLab.text = @"（先生）";
    }else if ([self.orderModel.gender isEqualToString:@"2"]) {
        self.genderLab.text = @"（女士）";
    }else {
        self.genderLab.text = @"（保密）";
    }
    
    self.genderLab.nim_left = self.nameLab.nim_right;
    [self.genderLab sizeToFit];
    self.genderLab.nim_centerY = self.nameLab.nim_centerY;
    
    self.phoneLab.text = [NSString stringWithFormat:@"联系方式 %@",self.orderModel.buyer_mobile];
    self.phoneLab.nim_left = self.nameLab.nim_left;
    self.phoneLab.nim_top = self.nameLab.nim_bottom + 3;
    [self.phoneLab sizeToFit];
    
    self.marginLine1.nim_left = self.nameLab.nim_left;
    self.marginLine1.nim_top = self.phoneLab.nim_bottom + 20;
    self.marginLine1.nim_width = self.bgView.nim_width - 2 * self.marginLine1.nim_left;
    self.marginLine1.nim_height = 1;
    
    if ([self.orderModel.order_status isEqualToString:@"2"]) {
        //已接单
        self.juJueBtn.frame = CGRectZero;
        self.jieDanBtn.frame = CGRectZero;
        self.chuDanBtn.nim_width = 48;
        self.chuDanBtn.nim_height = 30;
        self.chuDanBtn.nim_left = self.bgView.nim_width - 10 - self.chuDanBtn.nim_width;
        self.chuDanBtn.nim_top = (self.marginLine1.nim_top - self.chuDanBtn.nim_height) * 0.5;
        
        self.codeView.nim_left = self.nameLab.nim_left;
        self.codeView.nim_top = self.marginLine1.nim_bottom + 10;
        self.codeView.nim_width = self.bgView.nim_width - 2 * self.nameLab.nim_left;
        self.codeView.nim_height = 67;
        
        self.codeLab.text = [NSString stringWithFormat:@"%d",self.orderModel.order_code];
        [self.codeLab sizeToFit];
        self.codeLab.nim_left = (self.bgView.nim_width - self.codeLab.nim_width) * 0.5;
        
        self.codeTitleLab.text = @"取餐码";
        [self.codeTitleLab sizeToFit];
        self.codeTitleLab.nim_left = (self.bgView.nim_width - self.codeTitleLab.nim_width) * 0.5;
        self.codeTitleLab.nim_top = self.codeView.nim_top + (self.codeView.nim_height - 8 - self.codeTitleLab.nim_height);
        self.codeLab.nim_top = self.codeTitleLab.nim_top - self.codeLab.nim_height - 5;
        
        self.goodTitleLab.nim_top = self.codeView.nim_bottom + 20;
    }else {
        self.chuDanBtn.frame = CGRectZero;
        self.jieDanBtn.frame = CGRectMake(self.bgView.nim_width - 10 - 48, (self.marginLine1.nim_top - 30) * 0.5, 48, 30);
        self.juJueBtn.frame = CGRectMake(self.jieDanBtn.nim_left - 10 - 48, self.jieDanBtn.nim_top, self.jieDanBtn.nim_width, self.jieDanBtn.nim_height);
        
        self.goodTitleLab.nim_top = self.marginLine1.nim_bottom + 20;
    }
    
    self.goodTitleLab.text = @"商品信息";
    self.goodTitleLab.nim_left = self.nameLab.nim_left;
    [self.goodTitleLab sizeToFit];
    
    self.goodView.nim_left = self.nameLab.nim_left;
    self.goodView.nim_top = self.goodTitleLab.nim_bottom + 10;
    self.goodView.nim_width = self.bgView.nim_width - 2 * self.goodView.nim_left;
    self.goodView.nim_height = [self.goodView getHeightWithArray:self.orderModel.list];
    [self.goodView configUIWithArray:self.orderModel.list];
    
    
    self.remarkView.nim_left = self.nameLab.nim_left;
    self.remarkView.nim_top = self.goodView.nim_bottom + 10;
    self.remarkView.nim_width = self.bgView.nim_width - 2 * self.remarkView.nim_left;
    
    self.remarkLine.nim_left = self.remarkView.nim_left;
    self.remarkLine.nim_top = self.remarkView.nim_top;
    self.remarkLine.nim_width = 2;
    
    self.remarkTitleLab.text = @"备注";
    self.remarkTitleLab.nim_left = self.remarkLine.nim_right + 12;
    [self.remarkTitleLab sizeToFit];
    
    self.remarkLab.text = @"多给点一次性手套";
    self.remarkLab.nim_left = self.remarkTitleLab.nim_right + 10;
    self.remarkLab.nim_top = self.remarkView.nim_top + 12;
    self.remarkLab.nim_width = self.bgView.nim_width - 10 - 10 - self.remarkLab.nim_left;
    [self.remarkLab sizeToFit];
    
    self.remarkView.nim_height = self.remarkLab.nim_bottom + 12 - self.remarkView.nim_top;
    self.remarkLine.nim_height = self.remarkView.nim_height;
    self.remarkTitleLab.nim_centerY = self.remarkLab.nim_centerY;
    
    self.marginLine2.nim_left = self.nameLab.nim_left;
    self.marginLine2.nim_top = self.remarkView.nim_bottom + 20;
    self.marginLine2.nim_width = self.bgView.nim_width - 2 * self.marginLine2.nim_left;
    self.marginLine2.nim_height = 1;
    
    self.xiaoJiTitleLab.text = @"小计";
    self.xiaoJiTitleLab.nim_left = self.nameLab.nim_left;
    self.xiaoJiTitleLab.nim_top = self.marginLine2.nim_bottom + 20;
    [self.xiaoJiTitleLab sizeToFit];
    self.xiaoJiTitleLab.nim_width = (self.bgView.nim_width - 2 * self.xiaoJiTitleLab.nim_left) * 0.5;
    
    self.priceLab.nim_left = self.xiaoJiTitleLab.nim_right;
    self.priceLab.nim_centerY = self.xiaoJiTitleLab.nim_centerY;
    self.priceLab.text = [NSString stringWithFormat:@"￥%@",self.orderModel.pay_price];
    [self.priceLab sizeToFit];
    self.priceLab.nim_width = self.xiaoJiTitleLab.nim_width;
    
    self.pingTaiTitleLab.text = @"平台服务费";
    self.pingTaiTitleLab.nim_left = self.nameLab.nim_left;
    self.pingTaiTitleLab.nim_top = self.xiaoJiTitleLab.nim_bottom + 8;
    [self.pingTaiTitleLab sizeToFit];
    self.pingTaiTitleLab.nim_width = (self.bgView.nim_width - 2 * self.pingTaiTitleLab.nim_left) * 0.5;
    
    self.pingTaiLab.nim_left = self.pingTaiTitleLab.nim_right;
    self.pingTaiLab.nim_centerY = self.pingTaiTitleLab.nim_centerY;
    self.pingTaiLab.text = [NSString stringWithFormat:@"-￥%.2f",self.orderModel.fee];
    [self.pingTaiLab sizeToFit];
    self.pingTaiLab.nim_width = self.pingTaiTitleLab.nim_width;
    
    self.shouRuTitleLab.text = @"本单预计收入";
    self.shouRuTitleLab.nim_left = self.nameLab.nim_left;
    self.shouRuTitleLab.nim_top = self.pingTaiTitleLab.nim_bottom + 8;
    [self.shouRuTitleLab sizeToFit];
    self.shouRuTitleLab.nim_width = (self.bgView.nim_width - 2 * self.shouRuTitleLab.nim_left) * 0.5;
    
    self.shouRuLab.nim_left = self.shouRuTitleLab.nim_right;
    self.shouRuLab.nim_centerY = self.shouRuTitleLab.nim_centerY;
    self.shouRuLab.text = [NSString stringWithFormat:@"￥%.2f",self.orderModel.income];
    [self.shouRuLab sizeToFit];
    self.shouRuLab.nim_width = self.shouRuTitleLab.nim_width;
    
    self.orderStatusView.nim_left = self.nameLab.nim_left;
    self.orderStatusView.nim_top = self.shouRuTitleLab.nim_bottom + 20;
    self.orderStatusView.nim_width = self.bgView.nim_width - 2 * self.orderStatusView.nim_left;
    self.orderStatusView.nim_height = 55;
    
    self.infoLab.nim_left = self.orderStatusView.nim_left + 10;
    self.infoLab.nim_top = self.orderStatusView.nim_top + 10;
    self.infoLab.nim_width = self.orderStatusView.nim_width - 20;
    self.infoLab.nim_height = 13;
    self.infoLab.text = @"所属餐车：1号餐车  丨  01-15  12:12  下单";
    
    self.numberLab.nim_left = self.orderStatusView.nim_left + 10;
    self.numberLab.nim_top = self.infoLab.nim_bottom + 9;
    self.numberLab.nim_width = self.orderStatusView.nim_width - 20;
    self.numberLab.nim_height = 13;
    self.numberLab.text = [NSString stringWithFormat:@"订单编号：%@",self.orderModel.order_no];
    
    self.bgView.nim_height = self.orderStatusView.nim_bottom + 20;
    self.height = self.bgView.nim_bottom;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

@interface OrderGoodView ()

@end

@implementation OrderGoodView

- (CGFloat)getHeightWithArray:(NSArray <OrderGoodModel *>*)array {
    return 25 * array.count;
}

- (void)configUIWithArray:(NSArray <OrderGoodModel *>*)array {
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    __block CGFloat topY = 0;
    [array enumerateObjectsUsingBlock:^(OrderGoodModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        OrderGoodOneView *view = [[OrderGoodOneView alloc] initWithFrame:CGRectMake(0, topY, ScreenW - 30 - 20, 25)];
        view.model = obj;
        [self addSubview:view];
        topY = view.nim_bottom;
    }];
}

@end

@interface OrderGoodOneView ()

@property (nonatomic, strong) UILabel *nameLab;
@property (nonatomic, strong) UILabel *countLab;
@property (nonatomic, strong) UILabel *priceLab;

@end

@implementation OrderGoodOneView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.nameLab = [[UILabel alloc] initWithFrame:CGRectMake(0, (frame.size.height - 15) * 0.5, frame.size.width * 0.4, 15)];
        self.nameLab.font = [UIFont systemFontOfSize:14];
        self.nameLab.textColor = HexColor(0x585974);
        [self addSubview:self.nameLab];
        
        self.countLab = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLab.nim_right, self.nameLab.nim_top, frame.size.width * 0.2, self.nameLab.nim_height)];
        self.countLab.font = [UIFont systemFontOfSize:14];
        self.countLab.textColor = HexColor(0x585974);
        self.countLab.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.countLab];
        
        self.priceLab = [[UILabel alloc] initWithFrame:CGRectMake(self.countLab.nim_right, self.countLab.nim_top, self.nameLab.nim_width, self.nameLab.nim_height)];
        self.priceLab.font = [UIFont systemFontOfSize:14];
        self.priceLab.textColor = HexColor(0x585974);
        self.priceLab.textAlignment = NSTextAlignmentRight;
        [self addSubview:self.priceLab];
    }
    return self;
}

-(void)setModel:(OrderGoodModel *)model {
    _model = model;
    self.nameLab.text = model.title;
    self.countLab.text = [NSString stringWithFormat:@"x%d",model.amount];
    self.priceLab.text = [NSString stringWithFormat:@"¥%.2f",model.price];
}

@end
